package com.miyake.server.dsaccess;

import javax.jdo.PersistenceManager;

import com.miyake.server.DsNode;
import com.miyake.server.PMF;

public 	abstract class CreateSomething<T> {
	abstract T getObject();
	abstract void connect(T object, DsNode node);
	private String ret;
	public CreateSomething(String parentKey, String name, Integer type, Integer order) {
		DsNode thisNode = NodeManager.getInstance().createNode(parentKey, name, type, order);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
//			pm.makePersistent(thisNode);
			
			T object = getObject();
			pm.makePersistent(object);
			
			connect(object, thisNode);
			
			pm.makePersistent(thisNode);
			pm.makePersistent(object);
			ret = thisNode.getEncodedKey();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			pm.close();
		}			
	}
	
	public String getKey() {
		return ret;
	}
}
